﻿using System;
using System.Collections.Generic;

using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BingZi.HY.Core;
using NewLife.Web;
using BingZi;
using System.Collections;

namespace FrameWork.web
{
    public partial class LoginAndReg : BaseTemplatePage
    {

        #region

        /// <summary>
        /// 排序字段
        /// </summary>
        public String Type = WebHelper.GetStringParam(HttpContext.Current.Request, "type", "");
        /// <summary>
        /// 跳转后的URL
        /// </summary>
        public String ReturnUrl = WebHelper.GetStringParam(HttpContext.Current.Request, "ReturnUrl", "");
        #endregion
        #region "==方法=="

        /// <summary>
        /// 绑定页面元素
        /// </summary>
        private void BindPageItem()
        {
            if (!String.IsNullOrEmpty(Type))
            {
                if (Type == "reg")
                {
                    Reg();
                }
                else if (Type == "login")
                {
                    Login();
                }
                else if (Type == "LoginOut")
                {
                    LoginOut();
                }
             

            }
            Hashtable Puts = new Hashtable();
            Puts.Add("ReturnUrl", "ReturnUrl="+ReturnUrl);
            //要测试的方法
            Response.Write(ViewTemplate(EnumTemplateFile.LoginAndReg, Puts));
        }

        private void RedirectReturnUrl()
        {
             String url = HttpUtility.UrlDecode(WebHelper.GetStringParam(HttpContext.Current.Request, "ReturnUrl", ""));
            if (!String.IsNullOrEmpty(url.Trim()))
            {
                Response.Redirect(url);
            }
            else
            {
                Response.Redirect("index.aspx");
            }

        }


        #endregion

        #region 方法
        public void Reg()
        {
            sys_UserTable user = new sys_UserTable();
            user.U_Email = WebHelper.GetStringParam(HttpContext.Current.Request, "emailReg", "xxx@xx.com");
            user.U_EmailState = 1;
            user.U_LoginName =WebHelper.GetStringParam(HttpContext.Current.Request, "user_idReg", "xx");
            string passwordReg = WebHelper.GetStringParam(HttpContext.Current.Request, "passwordReg", "");
            string passwordconfirmReg = WebHelper.GetStringParam(HttpContext.Current.Request, "passwordconfirmReg", "#");
            if (passwordReg != passwordconfirmReg)
            {
                return;
            }
            else
            {
                user.U_Password = Common.inSQL(Common.md5(passwordReg, 32));
            }
            user.U_Phone = WebHelper.GetStringParam(HttpContext.Current.Request, "mobile", "");
            user.U_LastIP = Common.GetIPAddress();
            user.U_LoginType = "客户登录";
            user.U_Status = 0;
            user.U_LastDateTime = DateTime.Now;
            user.U_Address = WebHelper.GetStringParam(HttpContext.Current.Request, "city", "");
            if (!String.IsNullOrEmpty(user.U_Email) && !String.IsNullOrEmpty(user.U_LoginName))
            {
                if (sys_UserTable.Insert(user) > 0)
                {
                    RedirectReturnUrl();
                }
            }
                
        }
        public void Login()
        {
            MessageBox MBx = new MessageBox();
            MBx.M_Type = 1;
            MBx.M_IconType = Icon_Type.Alert;

            string U_LoginName = WebHelper.GetStringParam(HttpContext.Current.Request, "user_idLogin", "");
            string U_Password = WebHelper.GetStringParam(HttpContext.Current.Request, "passwordLogin", "");
            if (!String.IsNullOrEmpty(U_LoginName) && !String.IsNullOrEmpty(U_Password))
            {
                String UserKey = Common.GetIPAddress().Replace(".", "_");
                NewLife.QueryParam qp = new NewLife.QueryParam();
                qp.Where.Add(new NewLife.SearchParam(sys_UserTable._.U_Status, 0, NewLife.SearchType.Equal));
                qp.Where.Add(new NewLife.SearchParam(sys_UserTable._.U_LoginName, Common.inSQL(U_LoginName), NewLife.SearchType.Equal));
                int iInt = 0;
                List<sys_UserTable> lst = sys_UserTable.FindAll(qp, out iInt);
                if (iInt > 0 && lst[0] != null && lst[0].UserID > 0)
                {
                    if (lst[0].U_Status == 0)
                    {
                        new BingZiLogin().Signin(lst[0]);
                        RedirectReturnUrl();
                    }
                    else
                    {
                        //你的帐号已禁止登录
                        MBx.M_Title = "你的帐号已禁止登录！";
                        MBx.M_Body = "你的帐号已禁止登录,请联系本站客服。";
                    }
                }
                else
                {
                    //登录失败
                    MBx.M_Title = "登录失败！";
                    MBx.M_Body = "登录失败,请重新登录。";
                }
            }
            else
            {
                //帐号密码填写有误
                MBx.M_Title = "你的帐号密码填写有误！";
                MBx.M_Body = "你的帐号密码填写有误,请重新填写。";
            }
            MBx.M_IsView = true;
            MBx.M_WriteToDB = false;
            MBx.M_ButtonList.Add(new sys_NavigationUrl("返回", "index.aspx", "点击按钮返回首页！", UrlType.Href, true));
            EventMessage.MessageBox(MBx);

        }
        /// <summary>
        /// 退出
        /// </summary>
        public void LoginOut()
        {
            System.Web.Security.FormsAuthentication.SignOut();
            RedirectReturnUrl();
        }
        #endregion

        #region "==事件=="


        /// <summary>
        /// 页面加载
        /// </summary>
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {

                //绑定页面元素
                BindPageItem();


            }

        }




        #endregion
    }
}